package org.yzh.scheduler.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.yzh.scheduler.component.auth.AuthenticationFilter;
import org.yzh.scheduler.component.auth.AuthorizationInterceptor;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    public String[] authUrlPatterns() {
        return new String[]{
                "/schedule-job/*",
                "/job-log/*",
        };
    }

    //认证拦截器
    @Bean
    public AuthenticationFilter authenticationFilter() {
        return new AuthenticationFilter();
    }

    //鉴权过滤器
    @Bean
    public AuthorizationInterceptor authorizationInterceptor() {
        return new AuthorizationInterceptor();
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean(AuthenticationFilter authenticationFilter) {
        FilterRegistrationBean bean = new FilterRegistrationBean(authenticationFilter);
        bean.addUrlPatterns(authUrlPatterns());
        bean.setOrder(0);
        return bean;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizationInterceptor()).addPathPatterns(authUrlPatterns());
    }
}